/*
 * Copyright 2024 KylinSoft Co., Ltd.
 *
 * This program is free software: you can redistribute it and/or modify it under
 * the terms of the GNU General Public License as published by the Free Software
 * Foundation, either version 3 of the License, or (at your option) any later
 * version.
 *
 * This program is distributed in the hope that it will be useful, but WITHOUT
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
 * FOR A PARTICULAR PURPOSE. See the GNU General Public License for more
 * details.
 *
 * You should have received a copy of the GNU General Public License along with
 * this program. If not, see <https://www.gnu.org/licenses/>.
 */

#ifndef VECTORDB_H
#define VECTORDB_H

#include <Python.h>

#include <map>
#include <string>
#include <variant>
#include <vector>

struct VectorData {
    std::string id;
    std::vector<double> embedding = {};
    std::map<std::string, std::variant<std::string, int64_t>> metadata;
    std::string document;
};
class VectorDB {
public:
    explicit VectorDB(const std::string& databasePath);
    ~VectorDB();

    bool createCollection(const std::string& collectionName,
                          const std::string& searchAlgorithm);
    bool deleteCollection(const std::string& collectionName);
    std::vector<std::string> collections();
    bool hasCollections(const std::vector<std::string>& collections);
    bool addData(const std::string& collectionName,
                 const std::vector<VectorData>& data);
    bool updateData(const std::string& collectionName,
                    const std::vector<VectorData>& data);

    std::vector<VectorData> getData(
        const std::string& collectionName,
        const std::vector<std::string>& ids = {},
        const std::string& filter = std::string(),
        const std::string& filterDocument = std::string());

    bool deleteData(const std::string& collectionName,
                    const std::vector<std::string>& ids = {},
                    const std::string& filter = std::string(),
                    const std::string& filterDocument = std::string());

    std::vector<std::pair<VectorData, float>> queryData(
        const std::string& collectionName,
        std::vector<std::vector<double>> embeddings, int64_t numResults = 10);

    std::string generateInAndNotInFilter(
        const std::string& metadata, bool in,
        const std::vector<int64_t>& values) const;

    std::string generateContainsFilter(const std::string& document,
                                       bool contains) const;

private:
    void initializeChromaDB(const std::string& databasePath);
    std::vector<VectorData> convertPyObjectToVectorDatas(PyObject* pyObject);

    PyObject* dbInstance_ = nullptr;
};

#endif  // VECTORDB_H
